/**
 * Copyright (c) 2024 Huawei Technologies Co., Ltd.
 * This file is a part of the CANN Open Software.
 * Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

#include "../../../common/data_utils.h"
#ifndef ASCENDC_CPU_DEBUG
#include "acl/acl.h"
extern void select_with_bytes_mask_custom_do(uint32_t blockDim, void *l2ctrl, void *stream, uint8_t *srcGm, 
    uint8_t *maskGm, uint8_t *dstGm, uint8_t *workspace, uint8_t *tiling);
#else
#include "tikicpulib.h"
extern "C" __global__ __aicore__ void select_with_bytes_mask_custom(
    GM_ADDR srcGm, GM_ADDR maskGm, GM_ADDR dstGm, GM_ADDR workspace, GM_ADDR tiling);
#endif

constexpr uint32_t BLOCK_DIM = 1;
constexpr uint32_t FIRSTAXIS = 2;
constexpr uint32_t SRCLASTAXIS = 32;
constexpr uint32_t MASKLASTAXIS = 32;
constexpr uint32_t TILINGDATA_SIZE = 5;
constexpr uint32_t WORKSPACE_SIZE = 1024;

extern uint8_t *GenerateTiling(uint32_t firstAxis, uint32_t srcSize, uint32_t maskSize);

static bool CompareResult(const void *outputData, int64_t outSize)
{
    void *goldenData;
#ifdef ASCENDC_CPU_DEBUG
    goldenData = (uint8_t *)AscendC::GmAlloc(outSize);
#else
    CHECK_ACL(aclrtMallocHost((void **)(&goldenData), outSize));
#endif
    size_t goldenSize = outSize;
    bool ret = ReadFile("../output/golden.bin", goldenSize, goldenData, goldenSize);
    if (ret) {
        printf("ReadFile golden.bin success!\n");
    } else {
        printf("test failed!\n");
        return false;
    }
    constexpr float EPS = 1e-5;
    int64_t wrongNum = 0;

    for (int i = 0; i < outSize / sizeof(float); i++) {
        float a = (reinterpret_cast<const float *>(outputData))[i];
        float b = (reinterpret_cast<const float *>(goldenData))[i];
        float ae = std::abs(a - b);
        float re = ae / abs(b);
        if (ae > EPS && re > EPS) {
            printf("CompareResult golden.bin failed. Output[%d] is %lf, golden is %lf\n", i, a, b);
            wrongNum++;
        }
    }
#ifdef ASCENDC_CPU_DEBUG
    AscendC::GmFree((void *)goldenData);
#else
    CHECK_ACL(aclrtFreeHost(goldenData));
#endif
    if (wrongNum != 0) {
        return false;
    } else {
        printf("CompareResult golden.bin success!\n");
        return true;
    }
}

int32_t main(int32_t argc, char *argv[])
{
    uint32_t blockDim = BLOCK_DIM;
    size_t srcSize = FIRSTAXIS * SRCLASTAXIS * sizeof(float);
    size_t maskSize = FIRSTAXIS * MASKLASTAXIS * sizeof(bool);
    size_t tilingFileSize = TILINGDATA_SIZE * sizeof(uint32_t);
    size_t workspaceSize = WORKSPACE_SIZE * sizeof(float);

#ifdef ASCENDC_CPU_DEBUG
    uint8_t *srcInput = (uint8_t *)AscendC::GmAlloc(srcSize);
    uint8_t *output = (uint8_t *)AscendC::GmAlloc(srcSize);
    uint8_t *maskInput = (uint8_t *)AscendC::GmAlloc(maskSize);
    uint8_t *tiling = (uint8_t *)AscendC::GmAlloc(tilingFileSize);
    uint8_t *workspace = (uint8_t *)AscendC::GmAlloc(workspaceSize);

    ReadFile("../input/srcinput.bin", srcSize, srcInput, srcSize);
    ReadFile("../input/maskinput.bin", maskSize, maskInput, maskSize);

    memcpy_s(tiling, tilingFileSize, GenerateTiling(FIRSTAXIS, SRCLASTAXIS, MASKLASTAXIS), tilingFileSize);

    AscendC::SetKernelMode(KernelMode::AIV_MODE);
    ICPU_RUN_KF(select_with_bytes_mask_custom, blockDim, srcInput, maskInput, output, workspace, tiling); // use this macro for cpu debug

    WriteFile("../output/output.bin", output, srcSize);
    bool goldenResult = CompareResult(output, srcSize);
    if (goldenResult) {
        printf("test pass!\n");
    } else {
        printf("test failed!\n");
    }

    AscendC::GmFree((void *)srcInput);
    AscendC::GmFree((void *)output);
    AscendC::GmFree((void *)maskInput);
    AscendC::GmFree((void *)tiling);
    AscendC::GmFree((void *)workspace);
#else
    CHECK_ACL(aclInit(nullptr));
    aclrtContext context;
    int32_t deviceId = 0;
    CHECK_ACL(aclrtSetDevice(deviceId));
    CHECK_ACL(aclrtCreateContext(&context, deviceId));
    aclrtStream stream = nullptr;
    CHECK_ACL(aclrtCreateStream(&stream));

    uint8_t *srcInputHost, *outputHost, *maskInputHost, *workspaceHost, *tilingHost;
    uint8_t *srcInputDevice, *outputDevice, *maskInputDevice, *workspaceDevice, *tilingDevice;

    CHECK_ACL(aclrtMallocHost((void **)(&srcInputHost), srcSize));
    CHECK_ACL(aclrtMallocHost((void **)(&maskInputHost), maskSize));
    CHECK_ACL(aclrtMallocHost((void **)(&outputHost), srcSize));
    CHECK_ACL(aclrtMallocHost((void **)(&workspaceHost), workspaceSize));
    CHECK_ACL(aclrtMallocHost((void **)(&tilingHost), tilingFileSize));
    
    CHECK_ACL(aclrtMalloc((void **)&srcInputDevice, srcSize, ACL_MEM_MALLOC_HUGE_FIRST));
    CHECK_ACL(aclrtMalloc((void **)&maskInputDevice, maskSize, ACL_MEM_MALLOC_HUGE_FIRST));
    CHECK_ACL(aclrtMalloc((void **)&outputDevice, srcSize, ACL_MEM_MALLOC_HUGE_FIRST));
    CHECK_ACL(aclrtMalloc((void **)&workspaceDevice, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST));
    CHECK_ACL(aclrtMalloc((void **)&tilingDevice, tilingFileSize, ACL_MEM_MALLOC_HUGE_FIRST));

    ReadFile("../input/srcinput.bin", srcSize, srcInputHost, srcSize);
    ReadFile("../input/maskinput.bin", maskSize, maskInputHost, maskSize);

    float scalarValue;
    CHECK_ACL(aclrtMemcpy(tilingDevice, tilingFileSize, GenerateTiling(FIRSTAXIS, SRCLASTAXIS, MASKLASTAXIS), tilingFileSize, ACL_MEMCPY_HOST_TO_DEVICE));
    CHECK_ACL(aclrtMemcpy(srcInputDevice, srcSize, srcInputHost, srcSize, ACL_MEMCPY_HOST_TO_DEVICE));
    CHECK_ACL(aclrtMemcpy(maskInputDevice, maskSize, maskInputHost, maskSize, ACL_MEMCPY_HOST_TO_DEVICE));

    select_with_bytes_mask_custom_do(blockDim, nullptr, stream, srcInputDevice, maskInputDevice, outputDevice, workspaceDevice, tilingDevice);
    CHECK_ACL(aclrtSynchronizeStream(stream));
    CHECK_ACL(aclrtMemcpy(outputHost, srcSize, outputDevice, srcSize, ACL_MEMCPY_DEVICE_TO_HOST));

    WriteFile("../output/output.bin", outputHost, srcSize);

    bool goldenResult = CompareResult(outputHost, srcSize);
    if (goldenResult) {
        printf("test pass!\n");
    } else {
        printf("test failed!\n");
    }

    CHECK_ACL(aclrtFree(srcInputDevice));
    CHECK_ACL(aclrtFree(maskInputDevice));
    CHECK_ACL(aclrtFree(outputDevice));
    CHECK_ACL(aclrtFree(workspaceDevice));
    CHECK_ACL(aclrtFree(tilingDevice));

    CHECK_ACL(aclrtFreeHost(srcInputHost));
    CHECK_ACL(aclrtFreeHost(maskInputHost));
    CHECK_ACL(aclrtFreeHost(outputHost));
    CHECK_ACL(aclrtFreeHost(workspaceHost));
    CHECK_ACL(aclrtFreeHost(tilingHost));

    CHECK_ACL(aclrtDestroyStream(stream));
    CHECK_ACL(aclrtDestroyContext(context));
    CHECK_ACL(aclrtResetDevice(deviceId));
    CHECK_ACL(aclFinalize());
#endif
    return 0;
}
